<?php
namespace app\pc\controller;

use app\api\model\Users;

class UserInfo extends Common
{
    public function _initialize()
    {
        parent::_initialize();

    }

    //编辑用户的基本信息
    public function edit()
    {

        //如果当前的不是客服
        if(!$this->CurrentUserIsKf()){
            return ['error'=> 1, "msg"=> "您不是客服人员，无权管理"];
        }

        $input = input('post.');
        $input['client_id'] = input('get.client_id');
        if(!isset($input['client_id']) || empty($input['client_id']))
        {
            return  ['error'=> 1, "msg" => "没有指定用户进行设置"];
        }
        $userModel = new Users();
        $user = $userModel->user_info(['enc_client_id'=>$input['client_id']]);
        if(empty($user))
        {
            return ['error' => 1,"msg"=> "用户不存在"];
        }
        $input['client_id'] = $user['client_id'];
        $userInfoModel = new \app\api\model\UserInfo();
        $info = $userInfoModel->info(['client_id'=>$input['client_id']]);
        if($info['error'] != 0) return $info;
        //新增
        $result = [];
        if(empty($info['msg']))
        {
            $result =  $userInfoModel->add($input);
        }
        //编辑
        else
        {
            $result =  $userInfoModel->edit($input);
        }
        if($result['error'] == 0)
        {
            $result = array_merge(['client_id'=>$user['enc_client_id'],'nickname'=>$input['name']],$result);
        }
        return $result;
    }

    //当前用户信息
    public function info()
    {
        $input = input('post.');
        //如果当前的不是客服
        if(!$this->CurrentUserIsKf()){
            return ['error'=> 1, "msg"=> "您不是客服人员，无权管理"];
        }

        $userModel = new Users();
        $user = $userModel->user_info(['enc_client_id'=>$input['client_id']]);

        if(empty($user))
        {
            return ['error' => 1,"msg"=> "用户不存在"];
        }
        $input['client_id'] = $user['client_id'];

        $userInfoModel = new \app\api\model\UserInfo();
        return $userInfoModel->info(['client_id'=>$input['client_id']]);
    }
}